package com.server.chart;

import com.dao.chart.ChartsConfigMapper;
import com.dao.chart.ShowChartsMapper;
import com.github.pagehelper.PageInfo;
import com.model.bo.ChartsColumnConfig;
import com.model.bo.Screen;
import com.model.bo.ScreenStr;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.IndividualConfigInfo;
import com.model.vo.dataVo.PopAndIndDataInfo;
import com.config.BasesSource.DynamicDataSourceContextHolder;
import com.util.ScreenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/*
个人统计表
 */
@Service
public class ShowIndividualService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    ScreenUtil screenUtil;
    @Autowired
    ChartsUtilService chartsUtilService;

    public RepEntity showIndividual(IndividualConfigInfo individualConfigInfo, String token, int dashId){
        if (individualConfigInfo == null || "".equals(individualConfigInfo)){
            return new RepEntity(RepCode.Null);
        }
        PopAndIndDataInfo popAndIndDataInfo = new PopAndIndDataInfo();

        List<LinkedHashMap<String, Object>> targetValue = new ArrayList<>();
        //取表名
        int id = individualConfigInfo.getId();
        //获取列配置
        ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
        if (chartsColumn == null || "".equals(chartsColumn)){
            return new RepEntity(RepCode.Null);
        }
        popAndIndDataInfo.setChartsColumnConfig(chartsColumn);

        String tableName = chartsUtilService.getSqlStr(token, id, dashId);
        if ("".equals(tableName)){
            return new RepEntity(RepCode.NoAuthority);
    }

        int baseId = getChartsDataUtilService.getBaseId(id);
        System.out.println("切换数据库"+String.valueOf(baseId));

        try{
            if (!DynamicDataSourceContextHolder.isContainsDataSource(String.valueOf(baseId))) {
                //joinPoint.getSignature() ：获取连接点的方法签名对象
                System.out.println("数据源 " + String.valueOf(baseId) + " 不存在使用默认的数据源 -> ");
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                System.out.println("使用数据源：" + String.valueOf(baseId));
                DynamicDataSourceContextHolder.setDataSourceType(String.valueOf(baseId));
            }

            //取筛选列表
            List<Screen> screens = individualConfigInfo.getFilters();
            ScreenStr scr = new ScreenStr();
            String screen = "";
            String screenStr = "";
            if ("".equals(screens) || screens == null || screens.size() == 0){
                screen = "";
                screenStr = "";
            }else {
                scr = screenUtil.screensUtil(screens, "", "");
                screen = scr.getRet();
                StringBuilder sb = new StringBuilder(screen);
                screenStr = String.valueOf(sb .replace(1, 5, "where "));
            }

        String columnName = individualConfigInfo.getSortColumn();
        System.out.println("columnName:"+columnName);
        //查询其他列
        String columnListName = individualConfigInfo.getColumnListName().toString();
        String sort = individualConfigInfo.getSort();

        String tableListName = this.getString(columnListName);

        targetValue = showChartsMapper.getOtherData(tableListName, tableName, columnName, sort, screenStr, individualConfigInfo.getTestPage().enablePaging());
        PageInfo<LinkedHashMap<String, Object>> pageInfo = new PageInfo<>(targetValue);
        popAndIndDataInfo.setValueList(pageInfo);
    }catch (Exception e){
        e.printStackTrace();
        System.out.println("异常");
        return new RepEntity(RepCode.erro);
    }finally {
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
        return new RepEntity(RepCode.success, popAndIndDataInfo);
    }


    private String getString(String str){
        String st1 = "[";
        String st2 = "]";
        String strA = str.replace(st1, "");
        String strB = strA.replace(st2, "");
        System.out.println("str:"+ strB);
        return strB;
    }
}
